/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.main;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemCommandListener;
import javax.microedition.lcdui.Spacer;
import javax.microedition.lcdui.TextField;

import com.aroundj2me.uicomponents.ImageButton;
import com.aroundj2me.utility.ImageLoader;

/**
 * This form is used by users to login to Firstlife
 * @author Matthias Braunhofer
 */
public class LoginForm extends Form implements CommandListener, ItemCommandListener {
	private LoginController controller;
	private TextField userField;
	private TextField pwdField;
	private ImageButton loginButton;
	
	private static final Command loginCommand = new Command("Login", Command.OK, 1);
	private static final Command registerCommand = new Command("Register", Command.SCREEN, 1);
	private static final Command exitCommand = new Command("Exit", Command.EXIT, 1);
	
	public LoginForm(LoginController firstlife) {
		super("Login");
		this.controller = firstlife;
		initialize();
		loginButton.setDefaultCommand(loginCommand);
		loginButton.setItemCommandListener(this);
		addCommand(registerCommand);
		addCommand(exitCommand);
		this.setCommandListener(this);
	}
	
	private void initialize() {
		userField = new TextField("Username:", "", 30, TextField.ANY);
		pwdField = new TextField("Password:", "", 30, TextField.PASSWORD);
		loginButton = new ImageButton("Login", ImageLoader.getImage(ImageLoader.NEXT));
		loginButton.setLayout(ImageButton.LAYOUT_CENTER);
		this.append(userField);
		this.append(pwdField);
		this.append(new Spacer(this.getWidth(), 8));
		this.append(loginButton);
	}
	
	/**
	 * Clears all the content of the fields
	 * Saves memory instead of creating new object every time
	 */
	public void resetForm(){
		for(int i=0; i<this.size(); i++){
			if(this.get(i) instanceof TextField){
				TextField tmp = (TextField)this.get(i);
				tmp.setString("");
			}
		}
	}
	
	public void commandAction(Command c, Displayable d) {
		if(c == registerCommand){
			controller.showRegisterForm();
		} else if (c == exitCommand) {
			controller.notifyDestroyed();
		}
	}
	
	public void commandAction(Command c, Item i) {
		if(i==loginButton && c == loginCommand){
			controller.doLogin();
		}
		
	}

	/*
	 * Getters and Setters
	 */
	
	public TextField getPwdField() {
		return pwdField;
	}

	public void setPwdField(TextField pwd) {
		this.pwdField = pwd;
	}

	public TextField getUserField() {
		return userField;
	}

	public void setUserField(TextField user) {
		this.userField = user;
	}
}
